package com.yanqu.road.server.cmd.activity.biaoju.cross;

import com.yanqu.road.entity.activity.biaoju.UserBiaoJuData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.PlayerState;
import com.yanqu.road.logic.activity.biaoju.BiaoJuConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.BiaoJuProto;
import com.yanqu.road.server.cmd.activity.biaoju.task.BiaoJuSyncUserDataTask;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.biaoju.ActivityBiaoJuModule;
import com.yanqu.road.server.gameplayer.module.activity.fund.ActivityFundModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.biaoju.BiaoJuConfigMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_BIAOJU_USER_DATA_CROSS_BACK, desc = "玩家数据跨服返回")
public class BiaoJuUserDataSyncCrossBackCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
//        BiaoJuConfigMgr.addTask(new BiaoJuSyncUserDataTask(packet, player));

        BiaoJuProto.BiaoJuUserDataCrossBackSyncMsg syncMsg = BiaoJuProto.BiaoJuUserDataCrossBackSyncMsg.parseFrom(packet.getBytes());
        long userId = packet.getPlayerId();
        GamePlayer playerGet = GamePlayerMgr.getOnlinePlayer(userId);
        if (playerGet == null) {
            return;
        }

        playerGet.notifyListener(eGamePlayerEventType.BiaoJuExp.getValue(), syncMsg.getUserExp());
        playerGet.notifyListener(eGamePlayerEventType.BiaoJuExpRank.getValue(), syncMsg.getUserExp());
        playerGet.notifyListener(eGamePlayerEventType.BiaoJuUnionExpRank.getValue(), syncMsg.getUnionExp());
        BiaoJuConfig config = BiaoJuConfigMgr.getConfig();
        if (config != null && ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            UserBiaoJuData userData = playerGet.getModule(ActivityBiaoJuModule.class).getUserData();
            if (userData != null) {
                userData.setMoney(syncMsg.getMoney());
            }

            playerGet.getModule(ActivityFundModule.class).updateProgress(config.getActivityId(), syncMsg.getUnionExp());
        }
        if (playerGet.getState() == PlayerState.ONLINE) {
            playerGet.getModule(ActivityBiaoJuModule.class).syncRedPoint(syncMsg.getCanJoin());
        }
    }

    @Override
    public void sendData(GamePlayer player) {
    }

}
